package com.example.admin.myapplication.service;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.LauncherApps;
import android.os.IBinder;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.support.annotation.IntDef;
import android.telecom.Call;

import com.example.admin.myapplication.util.HttpUtil;
import com.example.admin.myapplication.util.Utility;

import java.io.IOException;

public class AutoUpdataService extends Service {
    public AutoUpdataService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
       return null;
    }

    @Override
    public int onStartCommand(Intent intent,int flags, int startId) {
        updateWeather();
        updateBingPic();
        AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
        int anHour =8*60*60*1000;
        long triggerAtTime = SystemClock.elapsedRealtime()+anHour;
        Intent i = new Intent(this,AutoUpdataService.class);
        PendingIntent pi = PendingIntent.getService(this,0,i,0);
        manager.cancel(pi);
        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pi);
        return super.onStartCommand(intent, flags, startId);
    }
    private  void  updateWeather(){
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String weatherString = prefs.getString("weather",null);
        if (weatherString !=null){
            Weather weather = Utility.handleWeatherResponse(weatherString);
            String weatherId = weather.basic.weatherId;
            String weatherUrl = "http://guolin.tech/api/weather?cityid="+weatherId+"&key=bc0418b57b2d4918819d3974ac1285d9";
            HttpUtil.sendOKHttppRequest(weatherUrl,new Callback(){
                @Override
                public void onResponse(Call call, Response response)throws IOException {
                    String responseText = response.body().string();
                    Weather weather1 = Utility.handleWeatherRessponse(responseText);
                    if (weather1 != null && "ok".equals(weather1.status) ){
                        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(AutoUpdataService.this).edit();
                        editor.putString("weather",responseText);
                        editor.apply();
                    }
                }
                @Override
                public void onFailure(Call call,IOException e){
                    e.printStackTrace();
                }
            });
        }
    }
private  void  updateBingPic(){
    String requestBingPic = "http://guolin.tech/api/bing_pic";
    HttpUtil.sendOKHttppRequest(requestBingPic, new Callback(){
        @Override
        public void onResponse(Call call,Response response)throws IOException{
            String bingPic=response.body().string();
           SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(AutoUpdataService.this).edit();
            editor.putString("bing_pic",bingPic);
            editor.apply();
        }
        @Override
        public  void onFailure(Call call,IOException e){
            e.printStackTrace();
        }
    });
}
}
